package com.we.connect.weconnect.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.List;

public class ParseContentUtils {

    private static final String romanticWords = "<romantic_words>";

    private static final String currentTime = "<current_time>";

    private static final String weather = "<weather>";

    private static final String cityId = "city";

    private static final String dateEd = "<date_ed>";

    private static final String date = "date";

    public static String parseContent(String content) throws IOException {
        String text;
        // 情话
        while (content.contains(romanticWords)) {
            do {
                text = (String) ((JSONObject) HttpClientUtils.get("https://api.shadiao.pro/chp").get("data")).get("text");
            } while (text.length() >= 30);
            content = content.replaceAll(romanticWords, text);
        }

        // 时间
        while (content.contains(currentTime)) {
            // 时间
            text = DateUtils.datePathChina();
            content = content.replaceAll(currentTime, text);
        }

        // 天气
        while (content.contains(weather) && content.contains(cityId)) {
            String index = content.substring(0, content.indexOf(weather));
            String parseContent = content.substring(index.length(), index.length() + 23);
            String cityId = content.substring(index.length() + 14, index.length() + 23);
            JSONObject result = HttpClientUtils.get("http://t.weather.itboy.net/api/weather/city/" + cityId);
            JSONObject today = (JSONObject) ((List) ((JSONObject) result.get("data")).get("forecast")).get(0);
            String info = today.get("week") + " " + ((JSONObject) result.get("cityInfo")).get("city") + "\n" + today.get("low") + " " + today.get("high") + " " + today.get("type") + "\n" + today.get("notice");
            content = content.replaceAll(parseContent, info);
        }

        // 已经发生的时间天数
        while (content.contains(dateEd) && content.contains(date)) {
            String index = content.substring(0, content.indexOf(dateEd));
            String parseContent = content.substring(index.length(), index.length() + 24);
            String date = content.substring(index.length() + 14, index.length() + 24);
            String nowDate = DateUtils.getDate();
            String data = DateUtils.getDateDifferences(date, nowDate);
            content = content.replaceAll(parseContent, data);
        }

        return content;
    }

    // 测试
    public static void main(String[] args) throws IOException {
        String s = parseContent("我们在一起已经<date_ed>date=2022-04-07天啦!");
        System.out.println(s);
    }
}
